// var postData = require('../../data/posts-data.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {},
  imgPath: "/images/...",
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var openid = wx.getStorageSync('openid')
    var _this = this
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/user/myCollection?openid=' + openid,
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        console.log(res)
        _this.setData({
          posts_list: res.data.data
        })
      }
    })
    // this.setData({
    //   post_content: postData.postList,
    //   num: 3
    // })
  },
  onPostTap(event) {
    var id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '../posts/post-detail/post-detail?id=' + id
    })
    console.log(event.currentTarget.dataset.id)
    console.log(url)
    // console.log(this.data.post_content[event.currentTarget.dataset.postid].collected)
  },
  onCollectTap: function (event) {
    console.log(event.currentTarget.dataset)
    var postId = event.currentTarget.dataset.postid
    var postIndex = event.currentTarget.dataset.index
    var openid = wx.getStorageSync('openid')
    var posts_list = this.data.posts_list
    var _this = this
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/resource/collection?id=' + postId + '&openid=' + openid,
      data: {},
      method: 'GET',
      success: function (res) {
        // success
        console.log(res)
        posts_list[postIndex].is_collection = !posts_list[postIndex].is_collection
        if (res.data.code == 200) {
          _this.setData({
            posts_list: posts_list
          })
          // console.log(_this.data.listsData[postIndex].is_collection)
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  onShowTap(event) {
    var postId = event.target.dataset.postid
    wx.navigateTo({
      url: 'post-detail/post-detail?id=' + postId
    })
  }
})